<template>
  <Header :title="'新建地址'"></Header>
  <van-address-list
    v-model="chosenAddressId"
    :list="list"
    :disabled-list="disabledList"
    disabled-text="以下地址超出配送范围"
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
    @select="select"
  />
</template>
<script setup>
import { ref } from "vue";
import { useRoute, onBeforeRouteLeave } from "vue-router";
import Header from "@/components/SHeader.vue";
import { showToast } from "vant";
import { addressList } from "@/api/address";
import router from "@/router";
const chosenAddressId = ref("1");
const list = ref([]);
const disabledList = ref([]);
const route = useRoute();
const getAddressList = async () => {
  const { data } = await addressList();
  list.value = data.res.map((item) => {
    return {
      id: item.address_id,
      name: item.username,
      tel: item.phone_number,
      address: `${item.province}${item.city}${item.county}${item.addressDetail}`,
      isDefault: !!item.isDefault,
    };
  });
};
getAddressList();
const select = (item) => {
  console.log(route.query);
  if (route.query.type) {
  } else {
    localStorage.setItem("address_id", item.id);
    router.go(-1);
  }
};
const onAdd = () => {
  router.push({ path: "add_address", query: { type: "add" } });
};
const onEdit = (item, index) => {
  console.log(item);
  router.push({ path: "add_address", query: { address_id: item.id, type: "edit" } });
};
</script>
